package com.example.camerademo

import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import androidx.activity.result.ActivityResultLauncher
import android.os.Bundle
import com.example.camerademo.R
import android.content.Intent
import android.view.View
import android.widget.Button
import com.example.camerademo.camera1.CameraPicActivity
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts.RequestMultiplePermissions
import com.dylanc.viewbinding.inflate
import com.example.camerademo.camera1.CameraPic2Activity
import com.example.camerademo.camera1.SecondActivity
import com.example.camerademo.camerax.CameraXActivity
import com.example.camerademo.countdownview.RecyViewActivity
import com.example.camerademo.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private val banding: ActivityMainBinding by inflate()


    private val mActivityResultLauncher: ActivityResultLauncher<String>? = null
    private var mActivityResultLaunchers: ActivityResultLauncher<Array<String>>? = null

    //注意：为了版本适配，读和写权限一起申请
    private val list = arrayOf(
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.CAMERA
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //mActivityResultLauncher = getP();
        mActivityResultLaunchers = p2
        //mActivityResultLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
        mActivityResultLaunchers!!.launch(list)
        banding.btnCameraPic.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, CameraPicActivity::class.java)
            //val intent = Intent(this@MainActivity, SecondActivity::class.java)
            startActivity(intent)
        })
        banding.btnCameraPic2.setOnClickListener {
            val intent = Intent(this@MainActivity, CameraPic2Activity::class.java)
            //val intent = Intent(this@MainActivity, SecondActivity::class.java)
            startActivity(intent)
        }
        banding.btnCameraVideo.setOnClickListener(View.OnClickListener {

        })


        banding.btnCameraX.setOnClickListener(View.OnClickListener {
            val intent = Intent(this@MainActivity, CameraXActivity::class.java)
            startActivity(intent)

        })


        banding.btnCountdown.setOnClickListener {
            val intent = Intent(this@MainActivity, RecyViewActivity::class.java)
            startActivity(intent)
        }


    }// User deny the permission.

    // User allow the permission.
    private val p: ActivityResultLauncher<String>
        private get() = registerForActivityResult(RequestPermission()) { granted ->
            if (granted) {
                // User allow the permission.
            } else {
                // User deny the permission.
            }
        }

    // mActivityResultLaunchers.launch(list);
    private val p2: ActivityResultLauncher<Array<String>>
        private get() = registerForActivityResult(RequestMultiplePermissions()) { result ->
            for ((key, value) in result) {
                println(".onActivityResult key=$key value=$value")
                if (!value) {
                    // mActivityResultLaunchers.launch(list);
                }
            }
        }
}